package br.edu.unilasalle.simon_api.dao.impl;

import java.util.Date;
import java.util.List;

import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;

import br.edu.unilasalle.simon_api.dao.IVendaDao;
import br.edu.unilasalle.simon_api.domain.Venda;
import br.edu.unilasalle.simon_api.util.DateUtil;

@Repository
public class VendaDao extends GenericDao<Venda, Long> implements IVendaDao {
	
	public List<Venda> findAllByPeriodo(Date dataIni, Date dataFim) {
        StringBuilder str = new StringBuilder();
        str.append("select new Venda(o.dataVenda, o.parcelas, o.valorTotal, ");
        str.append("o.funcionario.nome, o.funcionario.sobrenome, o.cliente.nome, o.cliente.sobrenome) ");
        str.append("from Venda o where o.dataVenda between :dataIni and :dataFim order by o.dataVenda, o.id");
        TypedQuery<Venda> q = entityManager.createQuery(str.toString(), Venda.class);
        q.setParameter("dataIni", dataIni);
        q.setParameter("dataFim", DateUtil.getLastHourOfDate(dataFim));
		return q.getResultList();
    }
	
}
